﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace ProjectSem40701.Admin
{
    public partial class SponsorEdit : System.Web.UI.Page
    {
        Guid sid;
        private const string SUCCESS = "success";
        private const string FAIL = "fail";

        protected void Page_Load(object sender, EventArgs e)
        {    
            if (!IsPostBack)
            {
                FillData();
            }
        }

        private void FillData()
        {
            try
            {
                sid = new Guid(Request.QueryString["sid"].ToString().Trim());
                AdminXSD.SponsorTableAdapters.tbl_SponsorTableAdapter dp =
                    new AdminXSD.SponsorTableAdapters.tbl_SponsorTableAdapter();
                DataTable dttbl = dp.Get_tbl_Sponsor_Select_All_By_SponsorID(sid);
                this.txtAddress.Text = dttbl.Rows[0]["SponsorAddress"].ToString();
                this.txtSponsorName.Text = dttbl.Rows[0]["SponsorName"].ToString();
                this.txtTel.Text = dttbl.Rows[0]["SponsorPhone"].ToString();
                this.txtEmail.Text = dttbl.Rows[0]["SponsorEmail"].ToString();
                this.txtDescription.Value = Server.HtmlDecode(dttbl.Rows[0]["SponsorDescription"].ToString());
                this.imgSponsorImage.ImageUrl = "~/images/" + dttbl.Rows[0]["SponsorImage"].ToString();

                foreach (ListItem item in this.cbbStatus.Items)
                {
                    if (item.Value == dttbl.Rows[0]["SponsorStatus"].ToString())
                    {
                        item.Selected = true;
                    }
                }
            }
            catch
            { 
                //show error here
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            try
            {
                string strImageSource;
                if (this.fileUpSponsorImage.HasFile)
                {
                    string str = DateTime.Now.ToString("ddMMyyyyhhmmssms");
                    this.fileUpSponsorImage.SaveAs(Server.MapPath("~/images/" + str +
                        this.fileUpSponsorImage.FileName.Trim()));
                    strImageSource = str + this.fileUpSponsorImage.FileName.Trim();
                }
                else
                {
                    strImageSource = this.imgSponsorImage.ImageUrl.Replace("~/images/", "");
                }

                sid = new Guid(Request.QueryString["sid"].ToString().Trim());                
                AdminXSD.SponsorTableAdapters.tbl_SponsorTableAdapter dp = 
                    new AdminXSD.SponsorTableAdapters.tbl_SponsorTableAdapter();
                int ok = dp.tbl_Sponsor_Update(this.txtSponsorName.Text.Trim(), this.txtAddress.Text.Trim(), this.txtTel.Text.Trim(), this.txtEmail.Text.Trim(),
                    Server.HtmlEncode(this.txtDescription.Value.Trim()), Convert.ToInt32(this.cbbStatus.SelectedValue.Trim()), sid, strImageSource);
                if (ok > 0)
                {
                    //bao thanh cong
                    Show_Dialog(SUCCESS);
                }
                else
                { 
                    //bao that bai
                    Show_Dialog(FAIL);
                }
            }
            catch
            {
                //show error here
                Show_Dialog(FAIL);
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                sid = new Guid(Request.QueryString["sid"].ToString());
                AdminXSD.SponsorTableAdapters.tbl_SponsorTableAdapter dp =
                    new AdminXSD.SponsorTableAdapters.tbl_SponsorTableAdapter();
                int ok = dp.tbl_Sponsor_Delete(sid);
                if (ok > 0)
                {
                    //bao thanh cong
                    Show_Dialog(SUCCESS);
                }
                else
                {
                    //bao that bai
                    Show_Dialog(FAIL);
                }
            }
            catch
            {
                //show error here
                Show_Dialog(FAIL);
            }
        }

        private void Close_Dialog()
        {
            switch (this.lblSuccess.Text)
            {
                case SUCCESS:
                    Response.Redirect("Sponsor.aspx");
                    break;
                case FAIL:
                    this.btnAdd_ModalPopupExtender.Enabled = false;
                    Response.Redirect("Sponsor.aspx");
                    break;
                default:
                    this.btnAdd_ModalPopupExtender.Enabled = false;
                    break;
            }
        }
        private void Show_Dialog(String Check)
        {
            switch (Check)
            {
                case SUCCESS:
                    this.ltrMessage.Text = "Thành công";
                    break;
                case FAIL:
                    this.ltrMessage.Text = "Thất bại";
                    break;
                default:
                    this.ltrMessage.Text = "Không thành công";
                    break;
            }
            this.lblSuccess.Text = Check;
            this.btnAdd_ModalPopupExtender.Enabled = true;
            this.btnAdd_ModalPopupExtender.Show();
        }
        protected void btnClose_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }

        protected void btnYes_Click(object sender, EventArgs e)
        {
            Close_Dialog();
        }
    }
}
